﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPSCrowdSource.DataStructures {
  
  /// <summary>
  /// Provides a data source that just constains a list of GPS data points.
  /// </summary>
  public class GPSListDataSource : IDataSource {
    
    #region Private
    protected List<GeoPosition> myPos = new List<GeoPosition>();
    protected int myIdx = 0;
    #endregion

    /// <summary>
    /// Create an empty list data source.
    /// </summary>
    public GPSListDataSource() {}

    /// <summary>
    /// Add a point to the list data source.
    /// </summary>
    public void Add(GeoPosition inPos) {
      lock(myPos) {
        myPos.Add(inPos);
        }
      }

    /// <summary>
    /// Read the next GPS value.
    /// </summary>
    /// <returns>null when EOF is reached.</returns>
    public GeoPosition ReadNext() {
      lock(myPos) {
        if(myIdx >= myPos.Count) return null;
        return  myPos[myIdx++];
        }
      }

    /// <summary>
    /// Returns the count or -1 if there is no way to know.
    /// </summary>
    public int Count {
      get { return myPos.Count; }
      }

    /// <summary>
    /// Close this stream source.
    /// </summary>
    public void Close() {
      lock(myPos) {
        try {
          myPos.Clear();
          myIdx = 0;
          }
        catch(Exception) {}
        }
      }

    /// <summary>
    /// Clear all contents from this list.
    /// </summary>
    public void Clear() {
      Close();
      } 

    public IEnumerator<GeoPosition> GetEnumerator() {
      lock(myPos) {
        return myPos.GetEnumerator();
        }
      }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
      lock(myPos) {
        return myPos.GetEnumerator();
        }
      }    

    }

  }
